package cn.bigcontainer.alt.workflow.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.bigcontainer.alt.common.exception.BigResultCode;
import cn.bigcontainer.alt.common.util.WoJsonKit;
import cn.bigcontainer.alt.meta.service.MetaService;
import cn.bigcontainer.alt.workflow.service.ImportExcelService;
import cn.bigcontainer.alt.workflow.service.MWorkflowMetaService;


@Controller
@RequestMapping("/meta/man")
public class ImportExcelController {

    @Resource
    private ImportExcelService importExcelService;

    @Resource
    private MetaService metaService;

    @Resource
    private MWorkflowMetaService workflowMetaService;


    /**
     * 跳转到导入页面
     *
     * @return
     */
    @RequestMapping("/loadMetaImport")
    public String loadMetaImport() {
        return "meta/man/import";
    }

    /**
     * 跳转到添加对象
     *
     * @return
     */
    @RequestMapping("/loadMetaAddObject")
    public String loadMetaAddObject() {
        return "meta/man/AddObject";
    }

    /**
     * 跳转到添加工作流
     *
     * @return
     */
    @RequestMapping("/loadMetaAddWorkflow")
    public String loadMetaAddWorkflow() {
        return "meta/man/AddWorkflow";
    }

    /**
     * 处理导入信息
     *
     * @param moduleName  模块名
     * @param fileContent excel
     * @param map
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @ResponseBody
    public BigResultCode importExcel(String moduleName, String objectName, MultipartFile fileContent, Map<String, Object> map) {
        importExcelService.importExcel(moduleName, objectName, fileContent);
        return BigResultCode.getSuccessCode();
    }

    /**
     * 处理添加对象信息
     *
     * @param moduleName           模块名
     * @param objectName           对象名称
     * @param objectPropertyConfig 对象配置
     * @return
     */
    @RequestMapping(value = "/loadMetaAddObject", method = RequestMethod.POST)
    @ResponseBody
    public BigResultCode loadMetaAddObject(String moduleName, String objectName, String objectPropertyConfig) {
        List<String> strings = WoJsonKit.readAsList(objectPropertyConfig, String.class);
        StringBuilder objectConfig = new StringBuilder();
        for (int i = 0; i < strings.size(); i++) {
            String s = strings.get(i);
            StringBuilder stringBuffer = new StringBuilder();
            for (int j = 0; j < s.length(); j++) {
                if (s.charAt(j) == '（') {
                    stringBuffer.append('(');
                } else if (s.charAt(j) == '）') {
                    stringBuffer.append(')');
                } else if (s.charAt(j) == '：') {
                    stringBuffer.append(':');
                } else if (s.charAt(j) == '，') {
                    stringBuffer.append(',');
                } else {
                    stringBuffer.append(s.charAt(j));
                }
            }
            if (i == 0) {
                objectConfig.append(stringBuffer);
            } else {
                objectConfig.append(";").append(stringBuffer);
            }
        }
        metaService.createMeta(moduleName, objectName, objectConfig.toString());
        return BigResultCode.getSuccessCode();
    }

    /**
     * 处理添加工作流信息
     *
     * @param moduleName           模块名
     * @param objectName           对象名称
     * @param objectPropertyConfig 对象配置
     * @return
     */
    @RequestMapping(value = "/loadMetaAddWorkflow", method = RequestMethod.POST)
    @ResponseBody
    public BigResultCode loadMetaAddWorkflow(String moduleName, String objectName, String objectPropertyConfig) {

        String objectProperty = objectPropertyConfig.replace("[]", "[\"]");
        StringBuilder task = new StringBuilder();
        String[] split2 = objectProperty.split("\"");
        for (String item : split2) {
            if (item.contains(",[")) {
                String replace = item.replace(",[", "");
                if (replace.contains("],")) {
                    task.append(replace.replace("],", "")).append(";");
                } else {
                    task.append(replace).append(";");
                }
            }
        }
        String[] taskSplit = task.toString().split(";");
        for (int i = 0; i < split2.length; i++) {
            int a = split2[i].indexOf("人");
            int b = split2[i].indexOf("说明");
            int c = split2[i].indexOf("结果");
            int d = split2[i].indexOf("时间");
            if (a > -1 && !split2[i].equals("")) {
                String substring = split2[i].substring(0, a);
                for (String s : taskSplit) {
                    if (substring.equals(s)) {
                        split2[i] = "";
                        break;
                    }
                }
            } else if (b > -1 && !split2[i].equals("")) {
                String substring = split2[i].substring(0, b);
                for (String s : taskSplit) {
                    if (substring.equals(s)) {
                        split2[i] = "";
                        break;
                    }
                }
            } else if (c > -1 && !split2[i].equals("")) {
                String substring = split2[i].substring(0, c);
                for (String s : taskSplit) {
                    if (substring.equals(s)) {
                        split2[i] = "";
                        break;
                    }
                }
            } else if (d > -1 && !split2[i].equals("")) {
                String substring = split2[i].substring(0, d);
                for (String s : taskSplit) {
                    if (substring.equals(s)) {
                        split2[i] = "";
                        break;
                    }
                }
            }

        }
        StringBuilder opc = new StringBuilder();
        for (String value : split2) {
            if (!value.equals("")) {
                opc.append(value).append("\"");
            }
        }
        String replace = opc.toString().replace("\",", ";");
        String replace1 = replace.replace("\"", "");
        String replace2 = replace1.replace("[]", "");
        String replace3 = replace2.replace(",[", "(属性:\"");
        String replace4 = replace3.replace("],", "\");");
        String replace5 = replace4.replace(",,", ";");
        String replace6 = replace5.replace("\";", "\"");
        String replace7 = replace6.replace(";;;", ";");
        String replace8 = replace7.replace(";;", ";");
        String replace9 = replace8.replace(";\"", "\"");

        String s;
        if (replace9.substring(replace9.length() - 1).equals("]")) {
            s = replace9.replace("]", "\")");
        } else {
            s = replace9.substring(0, replace9.length() - 1);
        }
        StringBuilder stringBuffer = new StringBuilder();
        for (int j = 0; j < s.length(); j++) {
            if (s.charAt(j) == '（') {
                stringBuffer.append('(');
            } else if (s.charAt(j) == '）') {
                stringBuffer.append(')');
            } else if (s.charAt(j) == '：') {
                stringBuffer.append(':');
            } else if (s.charAt(j) == '，') {
                stringBuffer.append(',');
            } else {
                stringBuffer.append(s.charAt(j));
            }
        }
        workflowMetaService.createMeta(moduleName, objectName, stringBuffer.toString());
        return BigResultCode.getSuccessCode();
    }
}
